#!/bin/bash

# blog-admin Jenkins 内联构建和部署脚本
# 可以直接在 Jenkins 的 Execute shell 中使用

set -e  # 遇到错误立即退出

# 使用 Jenkins 的 WORKSPACE 环境变量
WORKSPACE_DIR="${WORKSPACE:-/var/jenkins_home/workspace/blog-admin}"
PROJECT_DIR="${WORKSPACE_DIR}"

# 如果工作空间是仓库根目录，需要进入 blog-admin 子目录
if [ -d "${WORKSPACE_DIR}/blog-admin" ]; then
    PROJECT_DIR="${WORKSPACE_DIR}/blog-admin"
fi

echo "========================================"
echo "blog-admin 构建和部署"
echo "工作空间: $WORKSPACE_DIR"
echo "项目目录: $PROJECT_DIR"
echo "========================================"

# 进入项目目录
cd "$PROJECT_DIR" || {
    echo "错误: 无法进入项目目录: $PROJECT_DIR"
    exit 1
}

# 显示当前目录
echo "当前目录: $(pwd)"
echo "目录内容:"
ls -la

# 步骤1: 安装依赖
echo ""
echo "步骤1: 安装依赖..."
if [ ! -d "node_modules" ]; then
    echo "执行 npm install..."
    npm install
    echo "依赖安装完成"
else
    echo "node_modules 已存在，跳过安装"
fi

# 步骤2: 构建项目
echo ""
echo "步骤2: 构建项目..."
echo "执行 npm run build..."
npm run build

# 检查构建结果
BUILD_DIR="${PROJECT_DIR}/dist"
if [ ! -d "$BUILD_DIR" ]; then
    echo "错误: 构建目录不存在: $BUILD_DIR"
    exit 1
fi

if [ -z "$(ls -A $BUILD_DIR)" ]; then
    echo "错误: 构建目录为空: $BUILD_DIR"
    exit 1
fi

echo "构建完成，构建目录: $BUILD_DIR"
echo "构建文件列表:"
ls -la "$BUILD_DIR" | head -10

# 步骤3: 部署项目
echo ""
echo "步骤3: 部署项目..."

# 检查部署脚本
DEPLOY_SCRIPT="${PROJECT_DIR}/docker-start.sh"
if [ ! -f "$DEPLOY_SCRIPT" ]; then
    echo "警告: 部署脚本不存在: $DEPLOY_SCRIPT"
    echo "将使用内联部署命令..."
    
    # 内联部署命令
    CONTAINER_NAME="blog-admin"
    PORT=3000
    NGINX_CONF="${PROJECT_DIR}/nginx.conf"
    
    # 检测 Docker 命令
    DOCKER_CMD="docker"
    if ! docker ps >/dev/null 2>&1; then
        if sudo docker ps >/dev/null 2>&1; then
            DOCKER_CMD="sudo docker"
        else
            echo "错误: 无法访问 Docker"
            exit 1
        fi
    fi
    
    # 停止并删除旧容器
    echo "停止并删除旧容器..."
    $DOCKER_CMD stop $CONTAINER_NAME 2>/dev/null || true
    $DOCKER_CMD rm $CONTAINER_NAME 2>/dev/null || true
    
    # 检查端口冲突
    if $DOCKER_CMD ps --format "{{.Names}}\t{{.Ports}}" | grep -qE ":$PORT->"; then
        echo "警告: 端口 $PORT 已被占用"
    fi
    
    # 启动新容器
    echo "启动新容器..."
    if [ -f "$NGINX_CONF" ]; then
        $DOCKER_CMD run -d \
            --name $CONTAINER_NAME \
            --restart=unless-stopped \
            -p $PORT:80 \
            -v $BUILD_DIR:/usr/share/nginx/html:ro \
            -v $NGINX_CONF:/etc/nginx/conf.d/default.conf:ro \
            nginx:alpine
    else
        echo "警告: Nginx 配置文件不存在，使用默认配置"
        $DOCKER_CMD run -d \
            --name $CONTAINER_NAME \
            --restart=unless-stopped \
            -p $PORT:80 \
            -v $BUILD_DIR:/usr/share/nginx/html:ro \
            nginx:alpine
    fi
    
    # 等待容器启动
    sleep 3
    
    # 检查容器状态
    if $DOCKER_CMD ps --format "{{.Names}}" | grep -q "^${CONTAINER_NAME}$"; then
        echo "部署成功！"
        echo "容器名称: $CONTAINER_NAME"
        echo "服务端口: $PORT"
        echo "访问地址: http://服务器IP:$PORT"
    else
        echo "部署失败！容器未启动"
        $DOCKER_CMD logs $CONTAINER_NAME
        exit 1
    fi
else
    echo "执行部署脚本: $DEPLOY_SCRIPT"
    chmod +x "$DEPLOY_SCRIPT"
    "$DEPLOY_SCRIPT"
fi

echo ""
echo "========================================"
echo "构建和部署完成！"
echo "========================================"

